/***************************************************************************
                          StateMgr.cpp  -  description
                             -------------------
    begin                : October 29 2009
    copyright            : Copyright 2009, ACKERMANN Thibaut
    email                : tackerma@edu.univ-fcomte.fr
    website              : http://code.google.com/p/nover-project/
    license              : GNU GPL v3
 ***************************************************************************/
/* *************************************************************************
 *  This file is part of NOver-Project.
 *  
 *  NOver-Project is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  NOver-Project is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with NOver-Project.  If not, see <http://www.gnu.org/licenses/>.
 * *************************************************************************/


#include "StateMgr.hpp"


StateMgr::StateMgr() throw (MyException)
{
	windowSize = dimension2d<s32>(640, 480);
	
	irrdevice = createDevice(video::EDT_OPENGL, windowSize);
	
	if (!irrdevice)
	{
		throw MyException("Impossible d'instancier le irrlicht device", __FILE__, __LINE__);
	}
	
	irrdevice->setWindowCaption(L"NOver Project - 0.1 - Prototype 1");
	
	irrdriver = irrdevice->getVideoDriver();
	irrsmgr = irrdevice->getSceneManager();
	irrgui = irrdevice->getGUIEnvironment();
	
	irrdriver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
}


StateMgr::~StateMgr()
{
	while(states.size())
	{
		states.back()->exit();
		delete states.back();
		states.pop_back();
	}
}


void StateMgr::pushState(GameState* newState)
{
	if(!newState)
		return;
	
	if(states.size() != 0)
		states.back()->exit();
	
	states.push_back(newState);
	
	newState->enter();
}


void StateMgr::popState()
{
	if(states.size() >= 2)
	{
		states.back()->exit();
		states.pop_back();
		states.back()->enter();
	}
}


void StateMgr::startRender()
{
	if(!states.size())
		return;
	
	irrdevice->setEventReceiver(&myreceiver);
	
	// boucle principale.
	while(irrdevice->run())
	{
		if (irrdevice->isWindowActive())
		{
			irrdriver->beginScene(true, true, SColor(0, 100, 100, 100));
			
			irrsmgr->drawAll();
			
			irrgui->drawAll();
			
			//gestion du resize de la fenetre
			if(irrdriver->getScreenSize() != windowSize)
			{
				windowSize = irrdriver->getScreenSize();
				
				SEvent event;
				event.EventType = EET_USER_EVENT;
				event.UserEvent.UserData1 = windowSize.Height;
				event.UserEvent.UserData2 = windowSize.Width;
				
				myreceiver.OnEvent(event);
			}
			
			float time = 1.0/static_cast<float>(irrdriver->getFPS());
			
			if(states.size())
				states.back()->frame(time);
			
			irrdriver->endScene();
			
			// TODO : voir si ca ne gene pas trop.
			irrdevice->yield();
			
		} else {
			irrdevice->yield();
		}
	}

	irrdevice->drop();
}


